from launch import LaunchDescription
from launch_ros.actions import Node
# 封装终端指令相关类--------------
# from launch.actions import ExecuteProcess
# from launch.substitutions import FindExecutable
# 参数声明与获取-----------------
# from launch.actions import DeclareLaunchArgument
# from launch.substitutions import LaunchConfiguration
# 文件包含相关-------------------
# from launch.actions import IncludeLaunchDescription
# from launch.launch_description_sources import PythonLaunchDescriptionSource
# 分组相关----------------------
# from launch_ros.actions import PushRosNamespace
# from launch.actions import GroupAction
# 事件相关----------------------
# from launch.event_handlers import OnProcessStart, OnProcessExit
# from launch.actions import ExecuteProcess, RegisterEventHandler,LogInfo
# 获取功能包下share目录路径-------
from ament_index_python.packages import get_package_share_directory
from launch.actions import RegisterEventHandler #添加事件
from launch.event_handlers import OnProcessExit #添加事件
import os
""" 
    需求：演示Node使用
    构造函数参数说明：

        :param: executable 可执行程序；T
        :param: package 被执行的程序所属的功能包；T
        :param: name 节点名称；T
        :param: namespace 设置命名空间；T
        :param: exec_name 设置程序标签；F
        :param: parameters 设置参数；F
        :param: remappings 话题重映射
        :param: ros_arguments 为节点传参
                            --ros-args xx yy
        :param: arguments 为节点传参
                            xx yy --ros-args

 """

def generate_launch_description():
    # 启动自定义节点，进行服务请求和重试
    arm_client_mavros =  Node(
            package="mode_arm_service",
            executable="arm_client_mavros",  # 可执行程序
            name="arm_client_mavros",
            output="screen"
            #parameters=[{'param_name': 'param_value'}]  # 如果需要传递参数，可以添加这里
        )
     # 启动自定义节点，进行服务请求和重试
    mode_client_mavros =  Node(
            package="mode_arm_service",
            executable="mode_client_mavros",  # 可执行程序
            name="mode_client_mavros",
            output="screen"
            #parameters=[{'param_name': 'param_value'}]  # 如果需要传递参数，可以添加这里
        )
    # 注册事件处理器：当 mode_service 成功退出时启动 arm_service
    mode_service_success = RegisterEventHandler(
        OnProcessExit(
            target_action=mode_client_mavros,  # 监听 mode_service 节点
            # target_action=arm_service,  # 监听 mode_service 节点
            on_exit=[
                # 只有在 mode_service 正常退出(exit)时，才启动 arm_service
                arm_client_mavros
                # mode_service
            ]
        )
    )
    #t1 = Node(
     #       package="turtlesim",
      #      executable="turtlesim_node",
       #     exec_name="exec_name_t1",
        #    ros_arguments=["--remap","__ns:=/t2"],
            #ros2 run turtlesim turtlesim_node --ros-args --remap __ns:=/t3 rqt工具查看
         #   name="t1")#（功能包，可执行程序，名字）
    #t2 = Node(
    #        package="turtlesim",
    #        executable="turtlesim_node",
    #        respawn = True,
            #方式1
            #parameters=[{"background_r": 255,"background_g": 0,"background_b": 0}],  
            #方式2（常用），读取Yaml文件（通过Ymal绝对路径读取）
            #ros2 param dump t2 --output-dir src/cpp01_launch/config保存节点参数到文件，执行这条命令时要保证节点正在运行
            #parameters=["/home/ros2/ws02_tools/src/cpp01_launch/config/t2.yaml"],
            #优化：动态获取路径
            #parameters=[os.path.join(get_package_share_directory("cpp01_launch"),"config","t2.yaml")],

    #        name="t2")#（功能包，可执行程序，名字）

    return LaunchDescription([mode_client_mavros,mode_service_success])#注意这里，拿掉了mode_service，保留了mode_service_sucess来启动mode_service节点
    # return LaunchDescription([arm_service,mode_service,t2])